package com.moleq.adapter;

import java.util.List;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.moleq.base.EditableListener;
import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.dialog.FollowItemDialog;
import com.moleq.dialog.SelectionStringDialog;
import com.moleq.models.ItemDiscountSetting;
import com.moleq.mgpos.activities.R;
import com.moleq.util.ChangeMode;

public class ItemDiscountAdapter extends BaseAdapter implements OnResultListener {

	public static final String DISCOUNT_EDITED = "DiscountEdit";
	private LayoutInflater mInflater;
	private List<ItemDiscountSetting> listDiscount;
	private ItemDiscountSetting mItemDiscountSetting;
	private int selectedRow = -1;
	private EditableListener mEditableListener;
	private Context shareContext;
	ViewHolder holder = null;
	
	public ItemDiscountAdapter(Context context,List<ItemDiscountSetting> listDiscount){
		this.mInflater = LayoutInflater.from(context);
		this.shareContext = context;
		this.listDiscount = listDiscount;
	}
	
	public void setEditableListener(EditableListener listener){
		mEditableListener = listener;
	}
	
	public void setSource(List<ItemDiscountSetting> listDiscount){
		this.listDiscount = listDiscount;
	}
	
	public List<ItemDiscountSetting> getListItemDiscount(){
		return listDiscount;
	}
	
	public int getCount() {
		return listDiscount.size();
	}

	public Object getItem(int position) {
		return listDiscount.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
//		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.ds_discount_list, null);
			holder.et_discountno = (EditText) convertView.findViewById(R.id.et_discountno);
			holder.et_discountdesc = (EditText) convertView.findViewById(R.id.et_discountdesc);
			holder.et_discountamt = (EditText) convertView.findViewById(R.id.et_discountamt);
			holder.tv_followDiscountSet = (TextView) convertView.findViewById(R.id.tv_followDiscountSet);
			holder.iv_followDiscountSet = (ImageView) convertView.findViewById(R.id.iv_followDiscountSet);
			holder.btn_amtpercent = (Button) convertView.findViewById(R.id.btn_amtpercent);
			holder.tv_amtorpercent = (TextView) convertView.findViewById(R.id.tv_amtorpercent);
			holder.tv_percentSign = (TextView) convertView.findViewById(R.id.tv_percentSign);
			
			holder.et_discountdesc.addTextChangedListener(new dDescTextWatcher(holder));
			holder.et_discountdesc.setOnTouchListener(mOnTouchListener);
			holder.et_discountamt.addTextChangedListener(new dValueTextWatcher(holder));
			holder.et_discountamt.setOnTouchListener(mOnTouchListener);
			holder.iv_followDiscountSet.setOnClickListener(onClickListener);
			holder.btn_amtpercent.setOnClickListener(onClickListener);
			convertView.setTag(holder);
//		}else {
//			holder = (ViewHolder) convertView.getTag();
//		}
		
		ChangeMode.mode = 0;
		holder.et_discountno.setText(String.valueOf(position + 1));
		holder.et_discountdesc.setTag(position);
		holder.et_discountamt.setTag(position);
		holder.iv_followDiscountSet.setTag(position);
		holder.btn_amtpercent.setTag(position);
		String type = listDiscount.get(position).getDs_type();
		if (type.equals("A")) {
			type = "Amount";
			holder.tv_percentSign.setVisibility(View.INVISIBLE);
		}else {
			type = "%";
			holder.tv_percentSign.setVisibility(View.VISIBLE);
		}
		holder.btn_amtpercent.setText(type);
		
		Object desc = listDiscount.get(position).getDs_desc();
		if (desc != null) {
			holder.et_discountdesc.setText(desc.toString());			
		}
		
		double value = listDiscount.get(position).getDs_value();
		if (value != 0) {
			holder.et_discountamt.setText(String.format("%.2f",value));
		}
		ChangeMode.mode = 1;
		return convertView;
	}
	
	private OnTouchListener mOnTouchListener = new OnTouchListener(){
		public boolean onTouch(View v, MotionEvent event) {
			 if (event.getAction() == MotionEvent.ACTION_UP) {
				ChangeMode.mode = 1;
				selectedRow = (Integer) v.getTag();
		     }
		     return false;
		}
	};
	
	public OnClickListener onClickListener = new OnClickListener() {

		public void onClick(View view) {
			switch (view.getId()) {
			case R.id.iv_followDiscountSet:
				selectedRow = (Integer) view.getTag();
				mItemDiscountSetting = listDiscount.get(selectedRow);
//				DialogWindows(mItemDiscountSetting);
				
				FollowItemDialog mFollowItemDialog = new FollowItemDialog(shareContext, ItemDiscountAdapter.this);
				mFollowItemDialog.setSource(listDiscount, mItemDiscountSetting, selectedRow);
				mFollowItemDialog.show();
				break;
			case R.id.btn_amtpercent:
				selectedRow = (Integer) view.getTag();
				mItemDiscountSetting = listDiscount.get(selectedRow);
//				DialogWindows();
				
				SelectionStringDialog mSelectionStringDialog = new SelectionStringDialog(shareContext, ItemDiscountAdapter.this);
				mSelectionStringDialog.setSource(listDiscount, mItemDiscountSetting, selectedRow);
				mSelectionStringDialog.show();
				break;

			}
		}
	};
	
	class dDescTextWatcher implements TextWatcher {
	    public dDescTextWatcher(ViewHolder holder) {
	    	mHolder = holder;
	    }

	    private ViewHolder mHolder;
	    
	    public void onTextChanged(CharSequence s, int start, int before, int count) {
	    	int position = (Integer) mHolder.et_discountdesc.getTag();
	    	if (s != null && !"".equals(s.toString())) {
				listDiscount.get(position).setDs_desc(s.toString());
	    	}else {
				String temp = "";
				listDiscount.get(position).setDs_desc(temp);
			}
	    }

	    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
	    }

	    public void afterTextChanged(Editable s) {
	    	if(ChangeMode.mode == 1){
	    		listDiscount.get(selectedRow).setEdit(true);
				mEditableListener.onEdited(DISCOUNT_EDITED);
			}
	    }
	}
	
	class dValueTextWatcher implements TextWatcher {
	    public dValueTextWatcher(ViewHolder holder) {
	    	mHolder = holder;
	    }

	    private ViewHolder mHolder;
	    public void onTextChanged(CharSequence s, int start, int before, int count) {
	    	int position = (Integer) mHolder.et_discountamt.getTag();
	    	if (s != null && !"".equals(s.toString())) {
	    		if (!s.toString().equals(".")) {
	    			listDiscount.get(position).setDs_value(Double.valueOf(s.toString()));
	    		}
	    	}else {
				listDiscount.get(position).setDs_value(0);
			}
	    }

	    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
	    }

	    public void afterTextChanged(Editable s) {
	    	if(ChangeMode.mode == 1){
	    		listDiscount.get(selectedRow).setEdit(true);
				mEditableListener.onEdited(DISCOUNT_EDITED);
			}
	    }
	}
	
	public final class ViewHolder{
		public EditText et_discountno;
		public EditText et_discountdesc;
		public EditText et_discountamt;
		public ImageView iv_followDiscountSet;
		public TextView tv_followDiscountSet;
		public Button btn_amtpercent;
		public TextView tv_amtorpercent;
		public TextView tv_percentSign;
	}
	public void ScrollToRow(int row){
		selectedRow = row;
		this.notifyDataSetChanged();
	}
	public int GetSelectedIndex(){
		return selectedRow;
	}

	@Override
	public void onCloseWithReturn(Class<? extends MoleQBaseDialog> pramClass,
			int viewId, String returnvalue) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onReturnValue(int what, Object obj) {
		// TODO Auto-generated method stub
		listDiscount = (List<ItemDiscountSetting>) obj;
		notifyDataSetChanged();
		mEditableListener.onEdited(DISCOUNT_EDITED);
	}

	@Override
	public void onShowingMessageDlg(boolean isShowing) {
		// TODO Auto-generated method stub
		
	}
}
